<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { getUserCommunityList } from '@/api/community';
import { Community } from '@/api/community/type';
import { router } from '@/router';
import { showNotify } from 'vant';

const communityList = ref([] as Community[]);
const loading = ref(true);

onMounted(async () => {
    try {
        loading.value = true;
        const res = await getUserCommunityList();
        communityList.value = res.data || [];
    } catch (error) {
        console.error('加载我的社区失败:', error);
        showNotify({
            type: 'danger',
            message: '加载我的社区失败',
            position: 'top'
        });
    } finally {
        loading.value = false;
    }
})

const navigateToCommunity = (item: Community) => {
    router.push({
        name: "CommunityHome",
        query: {
            'id': item.id
        }
    });
}

const createCommunity = () => {
    router.push({
        name: "CommunityCreate"
    });
}
</script>

<template>
    <div class="my-community-section">
        <div class="section-header">
            <h2 class="section-title">
                <van-icon name="user-circle-o" class="section-icon" />
                我的社区
            </h2>
            
            <div class="header-actions">
                <van-button 
                    v-if="communityList.length > 0"
                    size="small" 
                    plain 
                    to="/community/my"
                >
                    查看全部
                </van-button>
                <van-button 
                    type="primary"
                    size="small"
                    icon="plus"
                    @click="createCommunity"
                >
                    创建社区
                </van-button>
            </div>
        </div>
        
        <!-- 加载中 -->
        <div v-if="loading" class="loading-container">
            <van-loading size="24px" vertical>加载中...</van-loading>
        </div>
        
        <!-- 空状态 -->
        <div v-else-if="communityList.length === 0" class="empty-container">
            <van-empty description="您还没有加入任何社区" />
            <van-button type="primary" size="small" @click="createCommunity">创建社区</van-button>
        </div>
        
        <!-- 社区列表 -->
        <div v-else class="community-grid">
            <div 
                v-for="item in communityList.slice(0, 8)" 
                :key="item.id" 
                class="community-card"
                @click="navigateToCommunity(item)"
            >
                <van-image 
                    class="community-avatar" 
                    fit="cover"
                    radius="8"
                    :src="item.url" 
                />
                <div class="community-name">{{ item.name }}</div>
            </div>
        </div>
    </div>
</template>

<style lang="scss" scoped>
.my-community-section {
    background-color: white;
    border-radius: 8px;
    padding: 16px;
    margin-bottom: 16px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
    
    .section-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 16px;
        
        .section-title {
            margin: 0;
            font-size: 18px;
            font-weight: 600;
            color: #323233;
            display: flex;
            align-items: center;
            
            .section-icon {
                margin-right: 8px;
                font-size: 20px;
            }
        }
        
        .header-actions {
            display: flex;
            gap: 8px;
        }
    }
    
    .loading-container,
    .empty-container {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        height: 120px;
        gap: 16px;
    }
    
    .community-grid {
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
        gap: 16px;
        
        .community-card {
            display: flex;
            flex-direction: column;
            align-items: center;
            cursor: pointer;
            transition: transform 0.2s;
            
            &:hover {
                transform: translateY(-4px);
            }
            
            .community-avatar {
                width: 64px;
                height: 64px;
                margin-bottom: 8px;
            }
            
            .community-name {
                font-size: 14px;
                text-align: center;
                color: #323233;
                max-width: 100%;
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
            }
        }
    }
}
</style>